package cn.itsource.ymcc.task;

import cn.itsource.ymcc.common.constant.RConst;
import cn.itsource.ymcc.common.constant.TaskConstants;
import cn.itsource.ymcc.domain.KillActivity;
import cn.itsource.ymcc.domain.KillCourse;
import cn.itsource.ymcc.service.IKillActivityService;
import cn.itsource.ymcc.service.IKillCourseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ActivityTask {
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private IKillActivityService killActivityService;
    @Autowired
    private IKillCourseService killCourseService;

    /**
     * 每天晚上12-06点钟 去发布当天的活动
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void courseActivityTask(){
        //你怎么得到今天的活动
        Date date = new Date();
        List<KillActivity> list = killActivityService.list(new LambdaQueryWrapper<KillActivity>()
                .eq(KillActivity::getPublishStatus, RConst.Num.INT_0)
                .le(KillActivity::getBeginTime, date)
                .ge(KillActivity::getEndTime, date));
        if(list.size() == 0){
            return;
        }

        //最终是发布课程到redis里面
        Map<String, KillCourse> allMap = new HashMap<>();   //秒杀课程id为key，秒杀课程对象为值
        for (KillActivity killActivity : list) {
            //得到当前活动的所有课程
            List<KillCourse> killCourseList = killCourseService.list(new LambdaQueryWrapper<KillCourse>()
                            .eq(KillCourse::getPublishStatus,RConst.Num.INT_0)
                    .eq(KillCourse::getActivityId, killActivity.getId()));
            for (KillCourse killCourse : killCourseList) {
                //发布到redis里面后 还要修改状态
                redisTemplate.opsForValue()
                        .set(TaskConstants.KILL_STORE+":"+killCourse.getId(),killCourse.getKillCount());

                allMap.put(killCourse.getId().toString(),killCourse);
                killCourse.setPublishTime(date);
                killCourse.setPublishStatus(RConst.Num.INT_1);

            }
            killCourseService.updateBatchById(killCourseList);

            //修改数据
            killActivity.setPublishTime(date);
            killActivity.setPublishStatus(RConst.Num.INT_1);
        }
        killActivityService.updateBatchById(list);
        //大key
        BoundHashOperations<String, Object, Object> hashOps =
                redisTemplate.boundHashOps(TaskConstants.KEY_KILL_COURSE);
        hashOps.putAll(allMap);

        System.out.println("定时任务已执行完成");
    }

    //还有个定时器  清除过期的活动

}
